home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / util / shell / NVX_wi1.lha / WhereIs.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  7KB  |  251 lines

  1. ;/* WhereIs - Execute me to compile me with Lattice.
  2. LC -b1 -cfistq -v -y WhereIs.c
  3. Blink FROM LIB:c.o,WhereIs.o TO WhereIs LIBRARY LIB:LC.lib,LIB:Amiga.lib NODEBUG
  4. quit
  5. */
  6.  
  7. /* ******************************************************************
  8. Program Name:        WhereIs  YET_ANOTHER_NVX_UTIL
  9. Author:                M.Meany ( STIGG // Nerve Axis )
  10. Description:        Searches a path for all occurrences of a file that
  11.                     matches a given pattern. Recursivley enters all
  12.                     sub-directories of specified search path.
  13. Current Version:    1.00
  14. Known Bugs:
  15. Comments:            SET TABS TO 4
  16.                     Requires dos.library v37 for case insensetive pattern
  17.                     matching, this means release 2.04 or better!
  18. Revision History:    v1.00    Initial Release
  19.  
  20. ********************************************************************* */
  21.  
  22. /* Searches a specified path for ALL occurences of a given file */
  23. /* Uses DOS pattern matching to check file discriptions */
  24. /* Recursivley enters nested directories as they are encountered */
  25.  
  26.                     /* ************************ */
  27.                     /*   Include Header Files    */
  28.                     /* ************************ */
  29.  
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <exec/exec.h>
  33. #include <libraries/dos.h>
  34. #include <libraries/dosextens.h>
  35. #include <proto/dos.h>
  36. #include <proto/exec.h>
  37.  
  38.                     /* ************************ */
  39.                     /*    Function Prototypes    */
  40.                     /* ************************ */
  41.  
  42. extern struct DOSBase *DOSBase;
  43.  
  44. void About( void );
  45. void CheckEntry( char *name, char *dir );
  46. void TackDir( char *dir );
  47. void UntackDir( char *dir );
  48. void AddFile( char *name, char *file );
  49.  
  50.                     /* ************************ */
  51.                     /*             Main()            */
  52.                     /* ************************ */
  53.  
  54. void main( int argc, char *argv[] )
  55. {
  56.  
  57. int i;
  58. LONG len;
  59. BYTE *tokens;
  60.  
  61. /* Exit now if called from WorkBench */
  62. if( !argc )
  63.     {
  64.     About();
  65.     Delay( 50*10 );
  66.     return;
  67.     }
  68.  
  69. /* We need at least release 2.04 (v37) of dos for this program */
  70. if ( ((struct Library *)DOSBase)->lib_Version < 37 )
  71.     {
  72.     printf( "Sorry, you need WorkBench 2.04 or later to run me!\n" );
  73.     return;
  74.     }
  75.  
  76. /* And at least 3 parameters (this also sorts out a ? parameter ) */
  77. if( argc < 3 )
  78.     {
  79.     About();
  80.     return;
  81.     }
  82.  
  83. /* Determine length of search string */
  84. len = 1 + (LONG)strlen( argv[1] );
  85. if( len > 1 )
  86.     {
  87.     /* Allocate memory for DOS to build tokens in */
  88.     len = 2 +(len<<1);
  89.     if( tokens = AllocVec( len, MEMF_PUBLIC|MEMF_CLEAR ) )
  90.         {
  91.         /* Build tokens */
  92.         if( ParsePatternNoCase( argv[1], tokens, len ) >= 0 )
  93.             {
  94.             /* Now step through each of the specified paths */
  95.             for( i=2; i<argc; i++ )
  96.                 {
  97.                 /* Tell user what path is being examined */
  98.                 printf( "\nSearching '›1m%s›0m'.\n", argv[i] );
  99.                 /* And search this path */
  100.                 CheckEntry( tokens, argv[i] );
  101.                 }
  102.             }
  103.         else
  104.             printf( "Error parsing search string!\n" );
  105.         FreeVec( tokens );
  106.         }
  107.     else
  108.         printf( "Error allocating a buffer for tokenised search string!\n" );
  109.     }
  110. else
  111.     printf( "You must specify a search pattern!\n" );
  112. }
  113.  
  114.                     /* ************************ */
  115.                     /*         Show Usage Text        */
  116.                     /* ************************ */
  117.  
  118. void About(void)
  119. {
  120. printf( "          ›1mWhereIs, by STIGG // Nerve Axis 1994.›0m\n");
  121. printf( "›1mUsage:›0m    WhereIs SearchPattern Path1 [Path2] [Path3]...[PathN]\n");
  122. printf( "›1mExamples:›0m WhereIs main.c dh0: dh1: dh2\n");
  123. printf( "          WhereIs *.asm dh0:\n");
  124. printf( "          WhereIs *.doc dh0:SomeUtility\n");
  125. printf( "          WhereIs main.? dh0:Code\n");
  126. printf( "          WhereIs main* dh0:CSource dh1:Code\n");
  127. printf( "          WhereIs *my* dh0: dh1: cd0:\n\n");
  128. printf( "Any valid AmigaDOS pattern will do for this program!\n\n" );
  129. printf( "Now try a friendly ›1mNerve Axis›0m BBS:\n" );
  130. printf( "   ›1mTrick Or Treat II›0m   +44 (0)1703 391797\n");
  131. printf( "   ›1m13th Hour›0m           +44 (0)1704 505845\n");
  132. printf( "   ›1mThe Edge›0m            +44 (0)1226 289303\n");
  133. printf( "   ›1mPower Plant›0m         +44 (0)1229 431590\n");
  134. }
  135.  
  136.                     /* ************************ */
  137.                     /* Checks Files/Enters Dirs    */
  138.                     /* ************************ */
  139.  
  140. /* Must be called with dir->A directory name, else no joy! */
  141.  
  142. void CheckEntry( char *name, char *dir )
  143. {
  144. struct FileInfoBlock *fib;
  145. struct FileLock *lock, *oldlock;
  146.  
  147. /* Add current dir name to global path name */
  148. TackDir( dir );
  149. /* Get a lock on this directory */
  150. if( lock = (struct FileLock *)Lock( dir, ACCESS_READ ) )
  151.     {
  152.     /* CD into this directory */
  153.     oldlock = (struct FileLock *)CurrentDir( (BPTR)lock );
  154.     /* Allocate memory for a File Info Block */
  155.     if( fib=AllocVec( sizeof( struct FileInfoBlock ), MEMF_CLEAR ) )
  156.         {
  157.         /* Get details on this directory */
  158.         if( Examine( (BPTR)lock, fib ) )
  159.             {
  160.             /* Now examine each entry in directory */
  161.             /* Note that we stay in this loop until all entries have been */
  162.             /* checked. If we call ourself, we still come back into this */
  163.             /* loop. Thats why a FIB is allocated each call and not set */
  164.             /* as a global variable! */
  165.             while( ExNext( (BPTR)lock, fib ) )
  166.                 {
  167.                 /* If next entry is a file, see if it fits pattern */
  168.                 if( fib->fib_DirEntryType < 0 )
  169.                     AddFile( name, fib->fib_FileName );
  170.                 else
  171.                     /* If its not a file, it a dir! Enter it (recurse)*/
  172.                     CheckEntry( name, fib->fib_FileName );
  173.                 }
  174.             }
  175.         /* Free File Info Block */
  176.         FreeVec( fib );
  177.         }
  178.     /* CD Back to original directory */
  179.     CurrentDir( (BPTR)oldlock );
  180.     /* And release the lock */
  181.     UnLock( (BPTR)lock );
  182.     }
  183. /* Pull last dir name from global path name */
  184. UntackDir( dir );
  185. }
  186.  
  187. static char dtrack[512]="";
  188. WORD dtpos = 0;
  189.  
  190.                     /* ************************ */
  191.                     /*  Add to Global Pathname    */
  192.                     /* ************************ */
  193.  
  194. void TackDir( char *dir )
  195. {
  196.  
  197. /* Add dir name to the path */
  198. while( *dir )
  199.     {
  200.     dtrack[ dtpos ] = *dir++;
  201.     dtpos++;
  202.     }
  203.  
  204. /* if last char was a : terminate the path name */
  205. if( dtrack[ dtpos-1 ] == ':' )
  206.     dtrack[ dtpos ] = '\0';
  207. /* else tack a / on to it */
  208. else
  209.     {
  210.     dtrack[ dtpos ] = '/';
  211.     dtpos++;
  212.     dtrack[ dtpos ] = '\0';
  213.     }
  214. }
  215.  
  216.                     /* *************************** */
  217.                     /* Remove from Global Pathname */
  218.                     /* *************************** */
  219.  
  220. void UntackDir( char *dir )
  221. {
  222. WORD i;
  223.  
  224. if( dtpos>2 )
  225.     {
  226.     /* Find penultimate / or : in path name */
  227.     for( i=dtpos-2; !(dtrack[i]=='/') && !(dtrack[i]==':') && i; i-- );
  228.     /* Now terminate the string just past delimiter */
  229.     if( dtrack[i]=='/' || dtrack[i]==':' ) { dtrack[i+1]='\0'; dtpos=i+1; }
  230.     /* If we failed, clear the global path name */
  231.     if( !i ) { dtrack[i]='\0'; dtpos=0; }
  232.     }
  233. else
  234.     {
  235.     /* Must be at root already, clear global pathname */
  236.     dtpos=0;
  237.     dtrack[0] = '\0';
  238.     }
  239. }
  240.  
  241.                     /* ************************ */
  242.                     /* Do Pattern Check on File */
  243.                     /* ************************ */
  244.  
  245. void AddFile( char *name, char *file )
  246. {
  247. /* If DOS says file fits, display its full path name */
  248. if( MatchPatternNoCase( name, file ) ) printf( "%s%s\n", dtrack, file );
  249. }
  250.  
  251.